package common

import (
	"log"

	"github.com/casbin/casbin/v2/model"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
)

func GetExcludePathMap() map[string]struct{} {
	excludePathMap := make(map[string]struct{})
	excludePathMap["/login"] = struct{}{}
	return excludePathMap
}

func InitCasbin() *casbin.Enforcer {
	modelStr := `
	[request_definition]
	r = sub, obj, act
	
	[policy_definition]
	p = sub, obj, act
	
	[role_definition]
	g = _, _
	
	[policy_effect]
	e = some(where (p.eft == allow))
	
	[matchers]
	m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
	`
	a, err := gormadapter.NewAdapter("mysql", "root:123456@tcp(localhost:3306)/test2", true)
	if err != nil {
		log.Fatalln("init casbin adapter error:", err.Error())
	}
	m, err := model.NewModelFromString(modelStr)
	if err != nil {
		log.Fatalln("init model error:", err.Error())
	}
	enforcer, err := casbin.NewEnforcer(m, a)
	if err != nil {
		log.Fatalln("init casbin fail:", err.Error())
	}
	return enforcer
}
